package zxc.com;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class IOTest {
    // 文件上传
    @Test
    public void upload() throws URISyntaxException, IOException, InterruptedException {
        Configuration c = new Configuration();
        String uri = "hdfs://node1:8020";
        // 创建文件系统对象（基于HDFS的文件系统）
        FileSystem fs = FileSystem.get(new URI(uri), c, "root");

        // 1.创建流
        FileInputStream f = new FileInputStream(new File("D:/input.txt"));
        // 2.输出流
        FSDataOutputStream fos = fs.create(new Path("/input1.txt"));
        // 3.拷贝
        IOUtils.copyBytes(f, fos, c);
        //4.关闭流
        IOUtils.closeStream(fos);
        IOUtils.closeStream(f);

        fs.close();
    }

    // 文件下载
    @Test
    public void download() throws URISyntaxException, IOException, InterruptedException {
        Configuration c = new Configuration();
        String uri = "hdfs://node1:8020";
        // 创建文件系统对象（基于HDFS的文件系统）
        FileSystem fs = FileSystem.get(new URI(uri), c, "root");

        // 1.创建流
        FSDataInputStream fio = fs.open(new Path("/input1.txt"));
        // 2.输出流
        FileOutputStream fos = new FileOutputStream(new File("D:/123.txt"));
        // 3.拷贝
        IOUtils.copyBytes(fio, fos, c);
        //4.关闭流
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fio);

        fs.close();
    }

    // 下载第一块
    @Test
    public void downloadPart1() throws URISyntaxException, IOException, InterruptedException {
        Configuration c = new Configuration();
        String uri = "hdfs://node1:8020";
        // 创建文件系统对象（基于HDFS的文件系统）
        FileSystem fs = FileSystem.get(new URI(uri), c, "root");

        // 1.创建流
        FSDataInputStream fio = fs.open(new Path("/jdk-8u391-linux-x64.tar.gz"));
        // 2.输出流
        FileOutputStream fos = new FileOutputStream(new File("D:/jdk-8u391-linux-x64.tar.gz.part1"));
        // 3.拷贝 只拷贝（128Mb）
        byte[] bytes = new byte[1024];
        for (int i = 0; i < 1024 * 128; i++) {
            fio.read(bytes);
            fos.write(bytes);
        }
        //4.关闭流
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fio);

        fs.close();
    }
    // 下载第一块
    @Test
    public void downloadPart2() throws URISyntaxException, IOException, InterruptedException {
        Configuration c = new Configuration();
        String uri = "hdfs://node1:8020";
        // 创建文件系统对象（基于HDFS的文件系统）
        FileSystem fs = FileSystem.get(new URI(uri), c, "root");

        // 1.创建流
        FSDataInputStream fio = fs.open(new Path("/jdk-8u391-linux-x64.tar.gz"));
        // 2读取指定位置
        fio.seek(1024*1024*128);
        // 3.输出流
        FileOutputStream fos = new FileOutputStream(new File("D:/jdk-8u391-linux-x64.tar.gz.part2"));
        // 4.拷贝
        IOUtils.copyBytes(fio,fos,c);
        // 5.关闭流
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fio);

        fs.close();
    }

}
